function creat(cr) {
    return document.createElement(cr);
}

function getid(id) {
    return document.getElementById(id);
}

function getcn(cn) {
    return document.getElementsByClassName(cn);
}

// 加格子
var xh;
var continer = getid("continer");
for (var i = 0; i < 10; i++) {
    var xh = creat("div");
    for (var j = 0; j < 10; j++) {
        var check = creat("div");
        check.setAttribute('X', i);
        check.setAttribute('Y', j);
        check.className = "check";
        xh.appendChild(check);
    }
    continer.appendChild(xh);
}
// setCookie('max',0);
var arz = getid("arz");
arz.innerText = "最高记录:"+parseInt(getCookie('max'));

var aa = ["#FFFF00", "#FFE1FF", "#C0FF3E", "orange"];
var arr = [];
var num = 0;
var arr1 = [];
var count = 0;
var max = 0;
var win;
var check = getcn("check");
for (var i = 0; i < check.length; i++) { //随机颜色
    var ra = parseInt(Math.random() * 4);
    check[i].style.background = aa[ra];
}

function cc () {
    for (let i = 0; i < check.length; i++) { //消除事件
        (function(i) {  
            check[i].onclick = function() {
                if (this.style.background != 'linear-gradient(135deg, lightgray, grey)') {
                    arr = [];
                    arr[num] = this;
                    bb();
                    ee();  
                }               
            }            
        }(i))
    }
    
}
cc();


var timer = setInterval(
    function dd () {
        for (var i = 0;i<check.length;i++) {
            if (check[i].style.background == "white") {
                var ra = parseInt(Math.random() * 4);
                check[i].style.background = aa[ra];
            }
            else {
                check[i].style.background = "linear-gradient(135deg,LightGray,Grey)";
            }
        }
        cc();
    }, 30000
)

//格子去除
function ee () {  
    if (arr.length != 1) { 
        count = count + arr.length*(arr.length-1);
        ff ();  //更新当前分数
        arr1 = [];
        for (var x in arr) {
            arr1[x] = arr[x].parentNode;
            arr[x].remove();
        }

        for (var k = 0; k < arr.length; k++) {
        for (var j = 0; j < arr1[k].childNodes.length; j++) {
            arr1[k].childNodes[j].setAttribute('X', arr1[k].childNodes[0].getAttribute("X"));
            arr1[k].childNodes[j].setAttribute('Y', j);
        }
        for (; j < 10; j++) {
            var check1 = creat("div");
            check1.setAttribute('X', arr1[k].childNodes[0].getAttribute("X"));
            check1.setAttribute('Y', j);
            check1.className = "check";
            check1.style.background = "white";
            arr1[k].appendChild(check1);
        }
    }
    }

    
}

// 找同类
function bb() {
    for (var j = 0; j < arr.length; j++) {
        var x = parseInt(arr[j].getAttribute("X"));
        var y = parseInt(arr[j].getAttribute("Y"));
        (function rankX(j) {
            var sss = x + y;
            for (let i = 0; i < check.length; i++) {
                var ss = 1;
                if (parseInt(check[i].getAttribute("Y")) == y) {
                    var s = parseInt(check[i].getAttribute("X")) + parseInt(check[i].getAttribute("Y"));
                    if ((s - sss == 1 || s - sss == -1) && (arr[j].style.background == check[i].style.background)) {
                        for (var k = 0; k < arr.length; k++) { //是否重复
                            if (check[i].getAttribute("X") == arr[k].getAttribute("X") && check[i].getAttribute("Y") == arr[k].getAttribute("Y")) {
                                ss = 0;
                                break;
                            }
                        }
                        if (ss != 0) {
                            arr[arr.length] = check[i];
                        }
                    }
                }
            }
        }(j));
        (function rankY(j) {
            var x = parseInt(arr[j].getAttribute("X"));
            var y = parseInt(arr[j].getAttribute("Y"));
            var sss = x + y;
            for (let i = 0; i < check.length; i++) {
                var ss = 1;
                if (parseInt(check[i].getAttribute("X")) == x) {
                    var s = parseInt(check[i].getAttribute("X")) + parseInt(check[i].getAttribute("Y"));
                    if ((s - sss == 1 || s - sss == -1) && (arr[j].style.background == check[i].style.background)) {
                        for (var k = 0; k < arr.length; k++) { //是否重复
                            if (check[i].getAttribute("X") == arr[k].getAttribute("X") && check[i].getAttribute("Y") == arr[k].getAttribute("Y")) {
                                ss = 0;
                                break;
                            }
                        }
                        if (ss != 0) {
                            arr[arr.length] = check[i];
                        }
                    }
                }
            }
        }(j));
    }

}

var arc = getid("arc");
function ff() {
    arc.innerText = "当前分数 "+count;
}

var end = getid("end");
var over = getid("over");
setTimeout(
    function gg() {
        clearInterval(timer);
        over.innerText = "最终得分"+count;
        end.style.display = "block";  
        
        if (count > parseInt(getCookie('max'))) {
            max = count;
            setCookie('max',count);
            arz.innerText = "最高记录:"+parseInt(getCookie('max'));
        }

    }
,90000)
setTimeout(
    function jj() {
        clearInterval(timer);    
    }
,89999)

var ars = getid("ars");
var arj = getid("arj");


rj = 90;
setInterval(
    function ii() {
        if(rj > 0) {
            arj.innerText = "结束时间:"+(--rj)+"s";
        }
        
    }
,1000)
setInterval(
    function hh() {
        ars.innerText = "刷新时间:"+(rj%30)+"s";
    }
,1000)



    //写cookies
    function setCookie(name, value) {
        var Days = 1;
        var exp = new Date();
        exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
        document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
    }
    //读cookies
    function getCookie(name) {
        var arr;
        var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg)) {
            return unescape(arr[2]);
        } else
            return null;
    }
    //删除cookise
    function delCookie(name) {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = getCookie(name);
        if (cval != null)
            document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    }